//
//  ViewController.m
//  6-13
//
//  Created by mac on 16-6-13.
//  Copyright (c) 2016年 hf. All rights reserved.
//

#import "ViewController.h"
#import "Phone.h"
#import "EditViewController.h"

@interface ViewController ()<EditDelegate>
{
    NSArray *da;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor redColor];
    NSString *path = @"/Users/mac/Desktop/00001.vcf";
    NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [vcf componentsSeparatedByString:@"\n"];
    NSMutableArray *re = [NSMutableArray new];
    Phone *p;
    for (NSString *item in arr) {
        if ([item hasPrefix:@"BEGIN"])
        {
            p = [Phone new];
        }
        else if([item hasPrefix:@"END"])
        {
            [re addObject:p];
        }
        else if([item hasPrefix:@"FN"])
        {
            NSLog(@"%@",item);
            NSArray *names = [item componentsSeparatedByString:@":"];
            NSString *name = names.lastObject;
            p.name = name;
        }
        else if ([item hasPrefix:@"TEL"])
        {
            NSArray *tels = [item componentsSeparatedByString:@":"];
            NSString *tel = tels.lastObject;
            p.tel = tel;
        }
    }
    da = re;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return da.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cd = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Phone *po = da[indexPath.row];
    cd.textLabel.text = po.name;
    cd.detailTextLabel.text = po.tel;
    return cd;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditViewController *en = [[EditViewController alloc]initWithStyle:UITableViewStyleGrouped];
    en.delegate = self;
    en.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:en animated:YES];
}

- (void)inputString:(NSString *)text
{
    NSLog(@"输入了：%@",text);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
